<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: ot_products_shipping.php 135 2013-09-22 07:30:55Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 135 $
* 	zuletzt geaendert von:	siekiera
* 	Datum:					22.03.2011
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */
   
class ot_products_shipping {
    var $title, $output, $db;

    function __construct() {
		global $price, $db;
		$this->code = 'ot_products_shipping';
		$this->title = MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_TITLE;
		$this->description = MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_DESCRIPTION;
		$this->enabled = ((MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_STATUS == 'true') ? true : false);
		$this->sort_order = MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_SORT_ORDER;
		$this->output = array();
		$this->db = $db;
    }
    
	function get_product_shipping_costs() {
		global $price;
		
		$products = $_SESSION['cart']->get_products();
		$costs = 0;
		$infos = array();
		
		for($i=0; $i<sizeof($products); $i++) {
			$row = $this->db->db_query("SELECT 
										p.products_shipping_costs AS costs,
										pd.products_name AS products_name
									FROM 	
										".TABLE_PRODUCTS." p
										LEFT JOIN ".TABLE_PRODUCTS_DESCRIPTION." AS pd USING (products_id)
									WHERE
										p.products_shipping_costs != 0
									AND
										p.products_id = '". $products[$i]['id']."'
									AND
										pd.language_id = '". $_SESSION['languages_id'] ."'");
			
			if($row->_numOfRows)  {
				while(!$row->EOF) {
					$costs += $price->format($row->fields['costs'] * $products[$i]['quantity'], false, 0, true);
					$infos[] = array('title' => $products[$i]['quantity'] .'x '. $row->fields['products_name'],
									'price' => $price->format($row->fields['costs'] * $products[$i]['quantity'], true, MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_TAX_CLASS, true),
									'price_plain' => $price->format($row->fields['costs'] * $products[$i]['quantity'], false, 0, true));
					$row->MoveNext();
				}
			}
		}
		$output = array('costs' => $costs,
						'infos' => $infos);
		return $output;
	}

    function process() {
		global $order, $price;
		require_once(DIR_FS_INC.'inc.calculate_tax.php');
      
		if(MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_STATUS == 'true') {
			switch (MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_DESTINATION) {
			  case 'national':
				if ($order->delivery['country_id'] == STORE_COUNTRY) $pass = true; break;
			  case 'international':
				if ($order->delivery['country_id'] != STORE_COUNTRY) $pass = true; break;
			  case 'both':
				$pass = true; break;
			  default:
				$pass = false; break;
			}

			if($pass == true) {
				$products_data = $this->get_product_shipping_costs();
				
				$tax = get_tax_rate(MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_TAX_CLASS, $order->delivery['country']['id'], $order->delivery['zone_id']);
				$tax_description = get_tax_description(MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_TAX_CLASS, $order->delivery['country']['id'], $order->delivery['zone_id']);

				if (MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_DETAILS == 'false') {
					if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 1) {
						$order->info['tax'] += calculate_tax($products_data['costs'], $tax);
						$order->info['tax_groups'][TAX_ADD_TAX."$tax_description"] += calculate_tax($products_data['costs'], $tax);
						$order->info['total'] += $products_data['costs'] + calculate_tax($products_data['costs'], $tax);
						$products_shipping_fee=add_tax($products_data['costs'], $tax);
					}
					if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 0 && $_SESSION['customers_status']['customers_status_add_tax_ot'] == 1) {
						$products_shipping_fee=$products_data['costs'];
						$order->info['tax'] += calculate_tax($products_data['costs'], $tax);
						$order->info['tax_groups'][TAX_NO_TAX."$tax_description"] += calculate_tax($products_data['costs'], $tax);
						$order->info['subtotal'] += $products_shipping_fee;
						$order->info['total'] += $products_shipping_fee;
					}
					if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 0 && $_SESSION['customers_status']['customers_status_add_tax_ot'] != 1) {
						$products_shipping_fee=$products_data['costs'];
						$order->info['subtotal'] += $products_shipping_fee;
						$order->info['total'] += $products_shipping_fee;
					}
					$output_title  = MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_OUTPUT_NAME.':';
					$this->output[] = array('title' => $output_title,
										  'text' 	=> $price->format($products_shipping_fee, true),
										  'value' => $products_shipping_fee);
										  
				} else {
					foreach ($products_data['infos'] as $info) {
						if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 1) {
							$order->info['tax'] += calculate_tax($info['price_plain'], $tax);
							$order->info['tax_groups'][TAX_ADD_TAX."$tax_description"] += calculate_tax($info['price_plain'], $tax);
							$order->info['total'] += $info['price_plain'] + calculate_tax($info['price_plain'], $tax);
							$products_shipping_fee = add_tax($info['price_plain'], $tax);
						}
						if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 0 && $_SESSION['customers_status']['customers_status_add_tax_ot'] == 1) {
							$products_shipping_fee = $info['price_plain'];
							$order->info['tax'] += calculate_tax($info['price_plain'], $tax);
							$order->info['tax_groups'][TAX_NO_TAX."$tax_description"] += calculate_tax($info['price_plain'], $tax);
							$order->info['subtotal'] += $products_shipping_fee;
							$order->info['total'] += $products_shipping_fee;
						}
						if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 0 && $_SESSION['customers_status']['customers_status_add_tax_ot'] != 1) {
							$products_shipping_fee = $info['price_plain'];
							$order->info['subtotal'] += $products_shipping_fee;
							$order->info['total'] += $products_shipping_fee;
						}
						$output_title = MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_OUTPUT_NAME.': ';
						$this->output[] = array('title' => $output_title .' '. $info['title'].': ',
											  'text' => $price->format($products_shipping_fee, true),
											  'value' => $products_shipping_fee);

					}
				}
			}
		}
	}

	function check() {
		if (!isset($this->_check)) {
			$check = $this->db->db_query("SELECT configuration_value FROM ".TABLE_CONFIGURATION." WHERE configuration_key = 'MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_STATUS'");
			$this->_check = $check->_numOfRows;
		}
		return $this->_check;
	}

	function keys() {
		  return array('MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_STATUS', 'MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_SORT_ORDER', 'MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_OUTPUT_NAME', 'MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_DETAILS', 'MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_DESTINATION', 'MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_TAX_CLASS');
	}

	function install() {
		  $this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, set_function, date_added) VALUES ('MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_STATUS', 'true', '6', '1','cfg_select_option(array(\'true\', \'false\'), ', now())");
		  $this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_SORT_ORDER', '31', '6', '2', now())");
		  $this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_OUTPUT_NAME', 'Sperrgutzuschlag', '6', '2', now())");
				$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, set_function, date_added) VALUES ('MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_DETAILS', 'true', '6', '1','cfg_select_option(array(\'true\', \'false\'), ', now())");
		  $this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, set_function, date_added) VALUES ('MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_DESTINATION', 'both','6', '6', 'cfg_select_option(array(\'national\', \'international\', \'both\'), ', now())");
		  $this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, use_function, set_function, date_added) VALUES ('MODULE_ORDER_TOTAL_PRODUCTS_SHIPPING_TAX_CLASS', '0','6', '7', 'get_tax_class_title', 'cfg_pull_down_tax_classes(', now())");
	}

	function remove() {
		$this->db->db_query("DELETE FROM ".TABLE_CONFIGURATION." WHERE configuration_key in ('".implode("', '", $this->keys())."')");
	}
}
?>